#container_name=$1
## 镜像名称
#image_name=$1
## 镜像tag
#image_tag=$2
#
## 判断容器是否存在
#if docker ps -a | grep $container_name | awk '{print $1}'; then
#  echo "容器 $container_name 存在"
#  if docker ps | grep $container_name | awk '{print $1}';then
#     echo "关闭正在运行的容器 $container_name"
#     docker stop `docker ps | grep $container_name | awk '{print $1}'`
#  else
#    echo "容器 $container_name 都已关闭"
#  fi
#  # 删除容器
#  echo "删除容器 $container_name"
#  docker rm `docker ps -a | grep $container_name | awk '{print $1}'`
#else
#  echo "容器 $container_name 不存在"
#fi
#
## 启动容器
#echo "启动容器 $container_name"
#if [ $container_name = "zzyl-admin" ]; then
#    docker run -d --restart=always --name $container_name -v /usr/local/zzyl-admin/logs:/home/ruoyi/logs -p 9000:9000 $image_name:$image_tag
#fi

#!/bin/bash

container_name=$1
image_name=$1
image_tag=$2

echo "===== 部署开始 ====="
echo "传入参数：container_name=$container_name, image_name=$image_name, image_tag=$image_tag"

# 查找所有匹配容器（包含已停止）
container_ids=$(docker ps -a -q -f name="^${container_name}$")
echo "查找到的所有容器ID（含停止）: $container_ids"

if [ -n "$container_ids" ]; then
  echo "容器 $container_name 存在"

  # 查找运行中的容器
  running_ids=$(docker ps -q -f name="^${container_name}$")
  echo "运行中容器ID: $running_ids"

  if [ -n "$running_ids" ]; then
    echo "关闭正在运行的容器 $container_name"
    docker stop $running_ids
  else
    echo "容器 $container_name 已关闭"
  fi

  echo "删除容器 $container_name"
  docker rm $container_ids
else
  echo "容器 $container_name 不存在"
fi

echo "启动容器 $container_name"
if [ "$container_name" = "zzyl-admin" ]; then
  echo "执行 docker run -d --restart=always --name $container_name -v /usr/local/zzyl-admin/logs:/home/ruoyi/logs -p 9000:9000 $image_name:$image_tag"
  docker run -d --restart=always --name $container_name -v /usr/local/zzyl-admin/logs:/home/ruoyi/logs -p 9000:9000 $image_name:$image_tag
fi

echo "===== 部署结束 ====="
